package com.its.controller;

import com.its.classOrder.pojo.ClassOrder;
import com.its.classOrder.service.ClassOrderService;
import com.its.domain.BaseResult;
import com.its.domain.CodeEnum;
import com.its.domain.PasswordDTO;
import com.its.member.pojo.Member;
import com.its.service.MemberService;
import org.apache.tomcat.util.net.openssl.ciphers.Authentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.SecurityContext;
import java.util.List;

@RestController
@RequestMapping("/member")
public class MemberController {
    //依赖注入业务层属性
    @Autowired
    private MemberService memberService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private ClassOrderService classOrderService;

    //新增会员
    @PostMapping("/add")
    public BaseResult addMember(@RequestBody Member member){
        return BaseResult.ok(memberService.addMember(member));
    }

    //删除会员
    @DeleteMapping("/delete")
    public BaseResult deleteMember(Integer memberAccount){

        return BaseResult.ok(memberService.delMember(memberAccount));
    }

    //修改会员信息
    @PutMapping("/update")
    public BaseResult updateMember(@RequestBody Member member){
        memberService.updateMember(member);
        return BaseResult.ok(memberService.selectMemberById(member.getMid()));
    }

    //根据Id查询会员信息
    @GetMapping("/findById")
    public BaseResult selectMemberById(Long mid){

        return BaseResult.ok(memberService.selectMemberById(mid));
    }

    //根据会员账号查询会员信息
    @GetMapping("/selMemberByMemberAccount")
    public BaseResult selMemberByMemberCount(Integer memberAccount){
        return BaseResult.ok(memberService.selMemberByMemberCountService(memberAccount));
    }

    //分页查询所有会员信息
    @GetMapping("/search")
    public BaseResult getAllMember(Integer page,Integer size){
        return BaseResult.ok(memberService.selectMemberByPage(page, size));
    }


    //声明单元方法：根据登录的用户名获取个人信息
    @GetMapping("/getMyInfo")
    public BaseResult getMyInfo(HttpServletRequest request){
        String memberAccount = request.getAttribute("memberAccount").toString();
        if(memberAccount == null){
            return BaseResult.error(CodeEnum.NOT_LOGIN_ERROR);
        }
        Member member = memberService.selMemberByMemberCountService(Integer.valueOf(memberAccount));
        return BaseResult.ok(member);
    }

    //声明单元方法，修改密码
    @PutMapping("/updatePassword")
    public BaseResult updatePassword(@RequestBody PasswordDTO passwordDTO, HttpServletRequest request){
        // 1. 获取登录用户账号
        String memberAccount = (String) request.getAttribute("memberAccount");
        if (memberAccount == null) {
            return BaseResult.error(CodeEnum.NOT_LOGIN_ERROR);
        }

        // 2. 查询会员信息
        Member member = memberService.selMemberByMemberCountService(Integer.valueOf(memberAccount));
        if (member == null) {
            return BaseResult.error(CodeEnum.USER_NOT_FOUND);
        }

        // 3. 校验旧密码
        if (!passwordEncoder.matches(passwordDTO.getOldPassword(), member.getMemberPassword())) {
            return BaseResult.error(CodeEnum.PASSWORD_ERROR);
        }

        // 4. 修改为新密码（加密后）
        member.setMemberPassword(passwordEncoder.encode(passwordDTO.getNewPassword()));
        memberService.updateById(member);

        return BaseResult.ok("密码修改成功");
    }

    //查询用户的课程报名表信息
    @GetMapping("/findByMemberAccount")
    public BaseResult findClassOrderByMemberAccount(HttpServletRequest request){
        String memberAccount = request.getAttribute("memberAccount").toString();
        if(memberAccount == null){
            return BaseResult.error(CodeEnum.NOT_LOGIN_ERROR);
        }

        //根据会员查询会员的报名信息
        List<ClassOrder> classOrders = classOrderService.selectClassOrderByMemberAccount(Integer.valueOf(memberAccount));

        return BaseResult.ok(classOrders);
    }

    //退课
    @DeleteMapping("/deleteClassOrder")
    public BaseResult deleteClassOrderByMemberAccount(Integer classOrderId){
        return BaseResult.ok(classOrderService.deleteClassOrder(classOrderId));
    }
}
